package kr.saeildamil.androidstudty.ch19;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class LooperTest extends Activity {
	int mMainValue;
	TextView mMainText;
	TextView mBackText;
	EditText mNumEdit;
	CalcThread thread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_loopertest);
		
		mMainText = (TextView) findViewById(R.id.ch19_loopertest_mainvalue);
		mBackText = (TextView) findViewById(R.id.ch19_loopertest_backvalue);
		mNumEdit = (EditText) findViewById(R.id.ch19_loopertest_number);
		
		thread = new CalcThread(mHandler);
		thread.setDaemon(true);
		thread.start();
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case 0:
				mBackText.setText("Square Result : " + msg.arg1);
				break;
			case 1:
				mBackText.setText("Root Result : " + ((Double)msg.obj).doubleValue());
				break;
			}
		}
	};
	
	public void mOnClick(View v){
		Message msg;
		switch(v.getId()){
		case R.id.ch19_loopertest_increase:
			mMainValue++;
			mMainText.setText("MainValue : " + mMainValue);
			break;
		case R.id.ch19_loopertest_square:
			msg = new Message();
			msg.what = 0;
			msg.arg1 = Integer.parseInt(mNumEdit.getText().toString());
			thread.mBackHandler.sendMessage(msg);
			break;
		case R.id.ch19_loopertest_root:
			msg = new Message();
			msg.what = 1;
			msg.arg1 = Integer.parseInt(mNumEdit.getText().toString());
			thread.mBackHandler.sendMessage(msg);
			break;
		}
	}
}










